package com.ruoyi.web.controller.api;

import com.ruoyi.business.domain.RechargeOrder;
import com.ruoyi.business.service.RechargeOrderService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * @author xx
 * @date 2020/5/1 6:59
 */
@RestController
@RequestMapping("api")
@Api("充值相关接口")
@Validated
public class ApiController extends BaseController {
    @Autowired
    private RechargeOrderService rechargeOrderService;
    @Autowired
    private SysUserService sysUserService;

    @ApiOperation(value = "下单", consumes = "application/x-www-form-urlencoded; charset=UTF-8", produces = "application/json", notes = "rechargeChannel:0-移动快充 1-移动慢充 2-联通快充 3-联通慢充 4-电信快充 5-电信慢充 \n" +
            "rechargeMoney: 0-30.00 1-50.00 2-100.00 3-200.00 4-300.00 5-500.00 \n" +
            "sign=md5(orderNum + mobile + rechargeChannel + rechargeMoney + userId + notifyUrl + key) \n" +
            "返回code=0代表系统接收成功，具体充值结果以异步通知为准 \n" +
            "Content-Type: application/x-www-form-urlencoded; charset=UTF-8")
    @PostMapping("pay")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "订单号", name = "orderNum", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(value = "手机号", name = "mobile", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(value = "充值通道 0-5", name = "rechargeChannel", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(value = "充值金额 0-5", name = "rechargeMoney", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(value = "用户id", name = "userId", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(value = "通知地址", name = "notifyUrl", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(value = "签名", name = "sign", required = true, dataType = "string", paramType = "query")
    })
    public AjaxResult pay(@NotBlank String orderNum, @NotBlank String mobile, @NotNull @Range(min = 0, max = 5) Integer rechargeChannel, @NotNull @Range(min = 0, max = 5)Integer rechargeMoney, @NotNull Long userId, @NotBlank String notifyUrl, @NotBlank String sign) {
        RechargeOrder order = new RechargeOrder();
        order.setOrderNum(orderNum);
        order.setMobile(mobile);
        order.setRechargeChannel(rechargeChannel);
        order.setRechargeMoney(rechargeMoney);
        order.setUserId(userId);
        order.setNotifyUrl(notifyUrl);
        AjaxResult result = new AjaxResult();
        SysUser sysUser = sysUserService.selectUserById(order.getUserId());
        if (sysUser == null) {
            result.put(AjaxResult.CODE_TAG, AjaxResult.Type.ERROR.value());
            result.put(AjaxResult.MSG_TAG, "用户不存在");
            return result;
        }
        if (!DigestUtils.md5DigestAsHex((order.getOrderNum() + order.getMobile() + order.getRechargeChannel() + order.getRechargeMoney() + order.getUserId() + order.getNotifyUrl() + sysUser.getKey()).getBytes()).equals(sign)) {
            result.put(AjaxResult.CODE_TAG, AjaxResult.Type.ERROR.value());
            result.put(AjaxResult.MSG_TAG, "签名错误");
            return result;
        }
        rechargeOrderService.insertRechargeOrder(order);
        result.put(AjaxResult.CODE_TAG, AjaxResult.Type.SUCCESS.value());
        result.put(AjaxResult.MSG_TAG, "操作成功");
        return result;
    }

    @ApiOperation(value = "查询", consumes = "application/x-www-form-urlencoded; charset=UTF-8", produces = "application/json", notes = "sign=md5(userId + orderNum + key) 返回code-1：充值中 2：成功 3：失败")
    @GetMapping("query")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户id", name = "userId", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(value = "订单号", name = "orderNum", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(value = "签名", name = "sign", required = true, dataType = "string", paramType = "query")
    })
    public AjaxResult query(@NotNull Long userId, @NotBlank String orderNum, @NotBlank String sign) {
        AjaxResult result = new AjaxResult();
        SysUser sysUser = sysUserService.selectUserById(userId);
        if (sysUser == null) {
            result.put(AjaxResult.CODE_TAG, AjaxResult.Type.ERROR.value());
            result.put(AjaxResult.MSG_TAG, "用户不存在");
            return result;
        }
        if (!DigestUtils.md5DigestAsHex((userId + orderNum + sysUser.getKey()).getBytes()).equals(sign)) {
            result.put(AjaxResult.CODE_TAG, AjaxResult.Type.ERROR.value());
            result.put(AjaxResult.MSG_TAG, "签名错误");
            return result;
        }
        RechargeOrder order = rechargeOrderService.selectByUserIdAndOrderNum(userId, orderNum);
        if (order == null) {
            result.put(AjaxResult.CODE_TAG, AjaxResult.Type.ERROR.value());
            result.put(AjaxResult.MSG_TAG, "订单不存在");
            return result;
        }
        result.put(AjaxResult.CODE_TAG, order.getState());
        result.put(AjaxResult.MSG_TAG, "查询成功");
        return result;
    }
}
